home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / cmln0286.arc / SIEVE.C < prev    next >
Text File  |  1986-02-03  |  1KB  |  51 lines

  1. /************************************************************************
  2. *                                    *
  3. *    sieve.c                                *
  4. *                                    *
  5. *    Example Program to test speed of C interpreters.        *
  6. *                                    *
  7. *    Finds: 1899 prime numbers                    *
  8. *                                    *
  9. *    Performs the sieve of Eratosthenes (from BYTE, Jan., 1983)    *
  10. *                                    *
  11. *    Computer Language Magazine [C Interpreter Wrap-Up, Feb., 1986]    *
  12. *                                    *
  13. ************************************************************************/
  14.  
  15.  
  16. #define TRUE 1
  17. #define FALSE 0
  18. #define SIZE 8190
  19.  
  20. char flags[SIZE+1];
  21.  
  22. main()
  23. {
  24.     register int i,k; 
  25.     /* int i,k; */ 
  26.     int prime,count,iter;
  27.  
  28.     printf("10 iterations\n");
  29.  
  30.     for (iter = 1; iter <= 10; iter++)      /* do program 10 times */
  31.     {
  32.         count = 0;                      /* initialize prime counter */
  33.         for (i = 0; i <= SIZE; i++)     /* set all flags true */
  34.             flags[i] = TRUE;
  35.         for (i = 0; i <= SIZE; i++) 
  36.         {
  37.             if (flags[i])           /* found a prime */
  38.             {
  39.                 prime = i + i + 3;      /* twice index + 3 */
  40.                 for (k = i + prime; k <= SIZE; k += prime)
  41.                     flags[k] = FALSE; 
  42.                               /* kill all multiples */
  43.                 count++;                /* primes found */
  44.             }
  45.         }
  46.     }
  47.  
  48.     printf("%d primes.\n",count);           /*primes found in 10th pass */
  49.     printf("sieve.c finished\n");
  50. }
  51.